package xian.woniu.service.impl;

import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xian.woniu.entity.*;
import xian.woniu.mapper.*;
import xian.woniu.mapper.OrdersMapper;
import xian.woniu.mapper.PatientMapper;
import xian.woniu.mapper.custom.CustomOrdersMapper;
import xian.woniu.service.IPatientService;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author SongHang
 * @time 2022/06/29 16:01:11
 */
@Service
@Transactional
public class PatientServiceImpl implements IPatientService {
    private PatientMapper patientMapper;

    public PatientServiceImpl(PatientMapper patientMapper) {
        this.patientMapper = patientMapper;
    }

    @Resource
    OrdersMapper ordersMapper;
    @Resource
    DoctorMapper doctorMapper;
    @Resource
    DepartmentMapper departmentMapper;
    @Resource
    OrdertypeMapper ordertypeMapper;
    @Resource
    AppointmentMapper appointmentMapper;

    /**
     * 查询所有患者
     * @return
     */
    @Override
    public List<Patient> findAll() {
        return patientMapper.selectByExample(null);
    }

    /**
     * 查询一个病人
     * @param pid
     * @return
     */
    @Override
    public Patient findOne(Integer pid) {
        return patientMapper.selectByPrimaryKey(pid);
    }

    //查询患者的签约记录
    @Override
    public List selSignRecord(Integer pid,Integer pageNow, Integer pageSize) {
        PageHelper.startPage(pageNow,pageSize);
        List signList=ordersMapper.selSignRecord(pid);
        return signList;
    }
    //查询患者的个人信息返回一个数组，主要用于方便填写表格
    @Override
    public List selPatinfoReturnList(Integer pid) {
        Patient patient = patientMapper.selectByPrimaryKey(Integer.valueOf(pid));
            List patinfoList=new ArrayList();
            patinfoList.add(patient);
            return patinfoList;
    }

    /**
     * 以下四个为患者个人信息表的增删查改
     * @param
     */
    @Override
    public Patient selPatinfo(Integer pid) {
        Patient patient=patientMapper.selectByPrimaryKey(pid);
        return patient;
    }
    @Override
    public void updatePatinfo(Patient patient) {
        patientMapper.updateByPrimaryKeySelective(patient);
    }
    @Override
    public void addPatinfo(Patient patient) {
        patientMapper.insertSelective(patient);
    }
    @Override
    public void delPatinfo(String pid) {
        patientMapper.deleteByPrimaryKey(Integer.valueOf(pid));
    }

    /**
     * 传入一个市的一个区，查询出这个区的所有医院
     */
    @Override
    public List selLocalDistrictHospital(String districtHospital) {
        List hospitalList=doctorMapper.selLocalDistrictHospital(districtHospital);
        return hospitalList;
    }

    /**
     * 查询当前医院可签约的医生团队
     */
    @Override
    public List selSignTeam(String dhospital) {
        List SignTeamlist=departmentMapper.selSignTeam(dhospital);
        return SignTeamlist;
    }

    /**
     * 查询当前医院可签约的医生团队的套餐
     */
    @Override
    public List selSignPackage(String depname) {
        List ordertypeList=ordertypeMapper.selSignPackage(depname);
        return ordertypeList;
    }

    /**
     * 根据套餐名称查询出套餐的id
     */
    @Override
    public Department selectByDepname(String depname) {
        Department department=departmentMapper.selectByDepname(depname);
        return department;
    }

    /**
     * 根据患者pid查询出所有和患者签约的医生信息
     */
    @Override
    public List selSignDocByPid(Integer pid) {
        List doclist = doctorMapper.selSignDocByPid(pid);
        return doclist;
    }

    /**
     * 查询出所有可签约医生的信息
     */
    @Override
    public List selSignDoc(Integer depid, String dhospital) {
        List docList=doctorMapper.selSignDoc(depid, dhospital);
        return docList;
    }

    /**
     * 添加一条患者的签约记录
     */
    @Override
    public Integer addSignRecord(Orders orders) {
        Integer onum=ordersMapper.addSignRecord(orders);
        return onum;
    }

    /**
     * 给患者续约套餐
     */
    @Override
    public Integer continueSign(Orders orders) {
        Integer conum=ordersMapper.continueSign(orders);
        return conum;
    }

    /**
     * 查询患者有没有同类型的订单
     */
    @Override
    public List selSameSign(Integer otid, Integer pid) {
        List ordersList=ordersMapper.selSameSign(otid, pid);
        return ordersList;
    }

    /**
     * 查询出患者的可预约服务时间
     */
    @Override
    public List selAppointmentTime() {
        List list = appointmentMapper.selAppointmentTime1();
        return list;
    }

    /**
     * 查询患者是否在该天已经预约该医生
     */
    @Override
    public Appointment selAppointmentRecord(Integer pid, Integer did, String atime) {
        Appointment appointment=appointmentMapper.selAppointmentRecord(pid, did, atime);
        return appointment;
    }

    /**
     * 患者预约服务
     */
    @Override
    public Void goAppointment(Appointment appointment) {
        appointmentMapper.insertSelective(appointment);
        return null;
    }

    /**
     * 查询患者的预约记录
     */
    @Override
    public List selAppointmentServiceRecord(Integer pid,Integer pageNow, Integer pageSize) {
        PageHelper.startPage(pageNow,pageSize);
        List list = doctorMapper.selAppointmentServiceRecord(pid);
        return list;
    }

    /**
     * 查询患者预约表的各种状态
     */
    @Override
    public List selAppointmentAstate() {
        List list = appointmentMapper.selAppointmentAstate();
        return list;
    }

    /**
     * 根据患者pid和预约状态查询出所有的预约记录
     */
    @Override
    public List selAppointmentByAstate(Integer pid, String astate,Integer pageNow, Integer pageSize) {
        List astate1 = doctorMapper.selAppointmentByAstate(pid, astate);
        return astate1;
    }

    /**
     * 查询出患者取消的这个服务是否已完成
     */
    @Override
    public Appointment selAppoinmentAstate(Integer pid, Integer did, String atime) {
        Appointment appointment=appointmentMapper.selAppoinmentAstate(pid,did,atime);
        return appointment;
    }


    //通过uid查个人信息
    @Override
    public Patient selPatientByUid(Integer uid) {
        PatientExample patientExample=new PatientExample();
        PatientExample.Criteria criteria = patientExample.createCriteria();
        criteria.andUidEqualTo(uid);
        List<Patient> list = patientMapper.selectByExample(patientExample);
        if(list.size()>0){
            return list.get(0);
        }
        return null;
    }

    @Override
    public Map<String, Integer> getNumByAge() {
        int children=0;
        int young=0;
        int middleage=0;
        int older=0;
        List<Patient> patientList = patientMapper.getNumByAge();
        for(Patient patient:patientList){
            if(patient.getPage()<=18){
                children++;
            }else if(18<patient.getPage()&&patient.getPage()<=35) {
                young++;
            }else if(35<patient.getPage()&&patient.getPage()<=60){
                middleage++;
            }else {
                older++;
            }
        }
        Map<String,Integer> map=new HashMap<>();
        map.put("children",children);
        map.put("young",young);
        map.put("middleage",middleage);
        map.put("older",older);
        return map;
    }


    @Override
    public List<Patient> getAllPatientInfo() {
        return patientMapper.getAllPatientInfo();
    }
}
